﻿using editor.cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor
{
    /// <summary>
    /// DynamicBeanControl.xaml 的交互逻辑
    /// </summary>
    public partial class DynamicBeanControl : UserControl
    {

        private cfg.DDynamicBean _bean;

        private Dictionary<int, DBean> _caches = new Dictionary<int, DBean>();
        public DynamicBeanControl(cfg.DDynamicBean bean)
        {
            InitializeComponent();
            this._bean = bean;
            this.DataContext = bean;

            _caches.Add(bean.CurTypeIndex, bean.Value);

        }

        private void types_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var index = types.SelectedIndex;
            if (index >= 0 && index != _bean.CurTypeIndex)
            {
                _caches[_bean.CurTypeIndex] = _bean.Value;
                if (!_caches.ContainsKey(index))
                    _bean.Value = _bean.NewData(_bean.Classes[index]);
                else
                    _bean.Value = _caches[index];
            }
        }

        public static bool InScroll { get; set; }

        private void types_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            InScroll = types.IsDropDownOpen;
        }
    }
}
